package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.clients.AddressClient;
import com.hmall.clients.ItemClient;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.order.common.BaseContext;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.OrderVo;
import com.hmall.order.service.IOrderService;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private AddressClient addressClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderLogMapper orderLogMapper;


    @Override
    @Transactional
    public long getorder(OrderVo orderVo) {
        long orderId = IdWorker.getId();
        Item item = itemClient.findById(orderVo.getItemId());
        long total= (long) (orderVo.getNum()*item.getPrice());
        Address address = addressClient.findById(orderVo.getAddressId());
        Order order = new Order();
        order.setId(orderId);
        order.setTotalFee(total);
        order.setPaymentType(3);
        order.setUserId(BaseContext.getThreadLocalId());
        order.setStatus(1);
        order.setCreateTime(new Date());
        orderMapper.insert(order);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setName(item.getName());
        orderDetail.setOrderId(orderId);
        orderDetail.setItemId(orderVo.getItemId());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setNum(orderVo.getNum());
        orderDetail.setImage(item.getImage());
        orderDetailMapper.insert(orderDetail);

        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address,orderLogistics);
        orderLogistics.setOrderId(orderId);
        orderLogistics.setMobile(address.getMobile());
        orderLogMapper.insert(orderLogistics);

        try {
            int stock = item.getStock() - orderVo.getNum();
            item.setStock(stock);
            itemClient.updateitem(item);
        } catch (Exception e) {
            throw new RuntimeException("库存不足");
        }
        return orderId;
    }
}
